package www.truongnha.com;

import java.util.ArrayList;
import java.util.HashMap;

import TruongNhaLib.DatabaseHandler;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class School extends ListActivity {
	 public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    Log.d("LOG", "Post 09: -2");
        setContentView(R.layout.school);
        Log.d("LOG", "Post 09: -1");
        //Create db object:
      	DatabaseHandler db = new DatabaseHandler(getApplicationContext());
      	
        // Get data for main Class
      	ArrayList<HashMap<String, String>> mainClass = db.getMainClassDetail();
      	ArrayList<HashMap<String, String>> classList = db.getClassDetails();

      	final ListAdapter classesAdapter = new SimpleAdapter(this, classList,
                R.layout.classes_item,
                new String[] { "classId", "name", "size", "subject", "mainteacher"}, new int[] {
                        R.id.cls_id ,R.id.cls_name, R.id.cls_size, R.id.cls_subject, R.id.cls_mainteacher });
        GridView grid = (GridView)findViewById(R.id.gridview);
        grid.setAdapter(classesAdapter);
        final Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
        grid.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting values from selected ListItem
				String classId = ((TextView) view.findViewById(R.id.cls_id)).getText().toString();
				String name = ((TextView) view.findViewById(R.id.cls_name)).getText().toString();
				toast.setText("Lớp " + name);
				toast.show();
				Intent in = new Intent(getApplicationContext(), Students.class);
				in.putExtra("CLASS_ID", classId);
				in.putExtra("CLASS_NAME", name);
				startActivity(in);
				}
		});
        
        final ListAdapter mainClassAdapter = new SimpleAdapter(this, mainClass, 
        		R.layout.classes_item,
        		new String[] { "classId", "name", "size", "subject", "mainteacher"}, new int[] {
                R.id.cls_id ,R.id.cls_name, R.id.cls_size, R.id.cls_subject, R.id.cls_mainteacher });
        setListAdapter(mainClassAdapter);
		// selecting single ListView item
		ListView lv = getListView();
		registerForContextMenu(lv);
		// Launching new screen on Selecting Single ListItem
		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting values from selected ListItem
				String classId = ((TextView) view.findViewById(R.id.cls_id)).getText().toString();
				String name = ((TextView) view.findViewById(R.id.cls_name)).getText().toString();
				toast.setText("Lớp " + name);
				toast.show();
				Intent in = new Intent(getApplicationContext(), Students.class);
				in.putExtra("CLASS_ID", classId);
				in.putExtra("CLASS_NAME", name);
				startActivity(in);
				}
		});
	 }//End oncreate
	@Override  
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
	super.onCreateContextMenu(menu, v, menuInfo);  
    	menu.add(0, 1, 0, "Nhập điểm bằng tay");
    	menu.add(0, 2, 0, "Nhập điểm bằng giọng nói");  
	    menu.add(0, 3, 0, "Đọc điểm cho học sinh");  
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		  switch(item.getItemId()) {
		  case 1:
		        Toast.makeText(this, "id =1", Toast.LENGTH_SHORT).show();
		        Intent in = new Intent(getApplicationContext(), SubmitMark.class);
		        startActivity(in);
		        return true;
		  case 2:
		        Toast.makeText(this, "id =2", Toast.LENGTH_SHORT).show();
		        return true;
		  case 3:
		        Toast.makeText(this, "id =3", Toast.LENGTH_SHORT).show();
		        return true;
		  default:
			  //Do nothing
			  return true;
		  }
	}//End
		  
/*	@Override
	public void onBackPressed() {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    builder.setMessage("Are you sure you want to exit?")
	           .setCancelable(true).setIcon(R.drawable.alert_icon).setTitle("Alert!")
	           .setPositiveButton("Có", new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int id) {
	                    School.this.finish();
	               }
	           })
	           .setNegativeButton("Không", new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int id) {
	                    dialog.cancel();
	               }
	           });
	    AlertDialog alert = builder.create();
	    alert.getWindow().getAttributes().windowAnimations = R.style.DialogAnimationTopDown;
	    alert.show();
	}//End onBackPressed
*/
}
